<?php
namespace app\admin\controller;

use think\Controller;
use think\Loader;
use app\admin\model\User as UserModel;

/**
 *用户登录控制器
 */
class Account extends Controller
{
    /**
     * 登录页显示
     * @return mixed
     */
    public function login(){

        echo json_encode(array('code'=>0,'msg'=>'','list'=>array('captcha_signin'=>config('captcha_signin'))));


        // 验证码是否启用
        $this->assign('captcha_signin',config('captcha_signin'));
        return $this->fetch();
    }
    /**
     * 管理员登录
     */
    public function dologin(){

        // 接收信息
        $data['name'] = trim(input('post.name'));
        $data['password'] = trim(input('post.password'));

        // 后台登陆数据校验
        $validate = Loader::validate('Account');
        if(!$validate->check($data)){
            exit(json_encode(array('code'=>0,'msg'=>$validate->getError(),'list'=>'')));
        }

        // 验证码是否启用
        if(config('captcha_signin')){
            // 验证码数据接收
            $verifycode = trim(input('post.verifycode'));
            // 验证码验证
            if(!captcha_check($verifycode)){
                exit(json_encode(array('code'=>0,'msg'=>'验证码错误','list'=>'')));
            }
        }


        // 验证用户
        $UserModel = new UserModel();
        $uid = $UserModel->login($data['name'], $data['password']);
        var_dump($uid);die;
        // 设置用户session
        session('admin',$admin);
        exit(json_encode(array('code'=>1,'msg'=>'登录成功')));
    }
    // 用户退出登录
    public function logout(){
        session('admin',null);
        exit(json_encode(array('code'=>1,'msg'=>'退出成功')));
    }
}